# vPresent is an immersive presentation creation and display application.
# Copyright (C) 2007 by Infiscape Corporation
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

from PyQt4 import QtCore, QtGui
import cylinderui
import gui.util.environment as environ

class Cylinder(QtGui.QWidget, cylinderui.Ui_CylinderBase):
   def __init__(self, cylinderElement, parent=None):
      QtGui.QWidget.__init__(self, parent)
      self.setupUi(self)

      self.mCylinderElement = cylinderElement
      self._updateUi()

   def setupUi(self, widget):
      cylinderui.Ui_CylinderBase.setupUi(self, widget)

      self.widgets = [self.mShowSides, self.mShowTop, self.mShowBottom,
                      self.mHeight, self.mRadius, self.mNumSides]

      self.connect(self.mShowSides, QtCore.SIGNAL("toggled(bool)"),
                   self.onStateChanged)
      self.connect(self.mShowTop, QtCore.SIGNAL("toggled(bool)"),
                   self.onStateChanged)
      self.connect(self.mShowBottom, QtCore.SIGNAL("toggled(bool)"),
                   self.onStateChanged)
      self.connect(self.mHeight, QtCore.SIGNAL("valueChanged(double)"),
                   self.onValueChanged)
      self.connect(self.mRadius, QtCore.SIGNAL("valueChanged(double)"),
                   self.onValueChanged)
      self.connect(self.mNumSides, QtCore.SIGNAL("valueChanged(int)"),
                   self.onValueChanged)

   def _updateUi(self):
      # Disable signals while changing values.
      for w in self.widgets:
         w.blockSignals(True)

      self.mHeight.setValue(self.mCylinderElement.vpresent_height)
      self.mRadius.setValue(self.mCylinderElement.vpresent_radius)
      self.mNumSides.setValue(self.mCylinderElement.vpresent_num_sides)

      self.mShowSides.setChecked(self.mCylinderElement.vpresent_show_sides)
      self.mShowTop.setChecked(self.mCylinderElement.vpresent_show_top)
      self.mShowBottom.setChecked(self.mCylinderElement.vpresent_show_bottom)

      for w in self.widgets:
         w.blockSignals(False)

   def _updateElement(self):
      self.mCylinderElement.vpresent_height = self.mHeight.value()
      self.mCylinderElement.vpresent_radius = self.mRadius.value()
      self.mCylinderElement.vpresent_num_sides = self.mNumSides.value()

      self.mCylinderElement.vpresent_show_sides = self.mShowSides.isChecked()
      self.mCylinderElement.vpresent_show_top = self.mShowTop.isChecked()
      self.mCylinderElement.vpresent_show_bottom = self.mShowBottom.isChecked()
      self.mCylinderElement._update(True)

      env = environ.Environment()
      env.updateGL()

   def onStateChanged(self, state):
      self.mNumSides.setEnabled(self.mShowSides.isChecked())
      self._updateElement()

   def onValueChanged(self, value):
      self._updateElement()
